package net.adetech.dcweb.core.dc.rest;

import com.alibaba.fastjson.JSONArray;
import net.adetech.dcweb.core.dc.entity.DcAuditComment;
import net.adetech.dcweb.core.dc.service.IDcAuditCommentService;
import net.adetech.utils.UtilsCommon;
import net.adetech.utils.spring.PageBean;
import net.adetech.web.utils.spring.BaseController;
import net.adetech.web.utils.UtilsUI;
import net.adetech.web.utils.UtilsWeb;
import net.adetech.utils.UtilsCondition;
import org.springframework.ui.Model;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by auto on 2016/10/21.
 */
@RestController
public class DcAuditCommentRestController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(DcAuditCommentRestController.class);

    @Autowired
    private IDcAuditCommentService iDcAuditCommentService;

    @PostMapping(value = "/dc/audit_comment", produces = "application/json")
    public String add(HttpServletRequest request, HttpServletResponse response) {
        DcAuditComment entity = this.wrapper(DcAuditComment.class, request);
        entity = iDcAuditCommentService.save(entity);
        response.setStatus(HttpServletResponse.SC_CREATED);
        opLog(request, logger, "增加", "");
        return UtilsUI.data(entity);
    }

    @GetMapping(value = "/dc/audit_comment/{id}", produces = "application/json")
    public String detail(@PathVariable Long id, HttpServletResponse response, HttpServletRequest request) {
        DcAuditComment entity = iDcAuditCommentService.find(id);
        if (null == entity) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return null;
        }
        opLog(request, logger, "查看", "");
        return JSONObject.toJSONString(entity);
    }

    @PutMapping(value = "/dc/audit_comment/{id}", produces = "application/json")
    public void update(HttpServletRequest request, @PathVariable Long id, HttpServletResponse response) {
        DcAuditComment entity = this.wrapper(DcAuditComment.class, request);
        entity.setId(id);
        int rs = iDcAuditCommentService.update(entity);
        opLog(request, logger, "修改", "");
        if (rs > 0) {
            response.setStatus(HttpServletResponse.SC_CREATED);
        } else {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    }

    @DeleteMapping(value = "/dc/audit_comment", produces = "application/json")
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        String idsStr = UtilsWeb.getString(request, "ids");
        if ("".equals(idsStr)) {
            return UtilsUI.tip("请传入ids参数");
        }
        String[] ids = idsStr.split(",");
        if (null != ids && ids.length > 0) {
            List
                    <Long> idsLong = new ArrayList<>(0);
            for (int i = 0; i < ids.length; i++) {
                idsLong.add(UtilsCommon.getLong(ids[i]));
            }
            iDcAuditCommentService.delete(idsLong);
        }
        opLog(request, logger, "删除", "");
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        return null;
    }

    @GetMapping(value = "/dc/audit_comments", produces = "application/json")
    public String page(HttpServletRequest request) {
        Integer nowPage = UtilsWeb.getInteger(request, "nowPage");
        Integer pageSize = UtilsWeb.getInteger(request, "pageSize");
        if (null == nowPage || nowPage <= 0) {
            return UtilsUI.tip("未传入nowPage参数，或者nowPage参数错误！");
        }
        if (null == pageSize || pageSize <= 0) {
            return UtilsUI.tip("请传入pageSize参数，或者pageSize参数错误！");
        }
        PageBean<DcAuditComment> pageBean = iDcAuditCommentService.page(nowPage,
                pageSize, UtilsCondition.getConditionEntity(request.getParameterMap()));
        opLog(request, logger, "分页查看", "");
        return UtilsUI.datagrid(pageBean);
    }

    @GetMapping(value = "/dc/audit_comments/getLog", produces = "application/json")
    public String getLog(HttpServletRequest request) {
        Integer nowPage = UtilsWeb.getInteger(request, "nowPage");
        if (nowPage == null)
            nowPage = 0;
        List<DcAuditComment> list = iDcAuditCommentService.getLog(nowPage);
        opLog(request, logger, "查看", "");
        return JSONObject.toJSONString(list);
    }
}